<!--
* @author luguoxiang
* @date 2021-09-07
* 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
-->
<template>
  <lg-modal
    ref="modalRef"
    :width="500"
    height="500"
    :header="$t('切换租户')"
    center
    append-to-body
    destroy-on-close
    @confirm="ok"
  >
    <t-radio-group v-model="tenantId">
      <t-radio v-for="x in props.options" :key="x.id" :value="x.id">{{ x.name }}</t-radio>
    </t-radio-group>
  </lg-modal>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { LgModal } from '@/soar';
import { useAccountStore } from '@/store';

const props = defineProps({
  options: Array,
});

const modalRef = ref();
const tenantId = ref();
const { user } = useAccountStore();

function ok() {
  if (tenantId.value === user.currentTenantId) {
    modalRef.value.close();
    return;
  }
  useAccountStore()
    .changeTenant(tenantId.value)
    .then(() => {
      modalRef.value.close();
      location.reload();
    });
}

function open() {
  tenantId.value = user.currentTenantId;
  modalRef.value.open();
}
defineExpose({ open });
</script>
